﻿/*
Esta libreria requiere que se incluya la libreria Common.js
*/

var ContextMenu_Instances = [];

function ContextMenu(id)
{
    // Public Functions
    this.id = id;
    this.width = "";
    this.acceptFunction = "";
    this.cssClass = "";
    
    // Private Properties
    this.items = [];
    this.count = 0;
    this.iswrited = false
    this.mainDiv;
    this.srcElement;
    this.activeItem;
    
    // Public Functions
    this.addItem = ContextMenu_AddItem;
    this.show = ContextMenu_Show;
    this.hide = ContextMenu_Hide;
    this.setPosition = ContextMenu_SetPosition;
    
    // Private Functions
    this.build = ContextMenu_Build;
        
    ContextMenu_Instances[id] = this;
}

function ContextMenu_AddItem(id, description) 
{
    if(this.iswrited)
        return;
        
    this.items[this.count] = new Array(id, description);
    this.count++;
}

function ContextMenu_Show(src, ev)
{
    this.srcElement = src;
    
    // deshabilito el menu contextual de la pagina
    document.oncontextmenu = new Function("return false;");
    
    if(this.iswrited) {
        this.setPosition(ev);
        this.mainDiv.style.display = "";
    }
    else {
        this.build(ev);
    }

    Common.bringToFront(this.mainDiv);
}

function ContextMenu_Hide() 
{
    if(this.iswrited) {
        this.mainDiv.style.display = "none";
    }
    
    // habilito el menu contextual de la pagina
    document.oncontextmenu = null;
}

function ContextMenu_Build(ev)
{
    this.mainDiv = document.createElement("div");
    this.mainDiv.id = "mainDiv_" + this.id;
    this.mainDiv.className = this.cssClass;
    this.mainDiv.setAttribute("isContext", "yes");

    Common.setEventHandler(this.mainDiv, "onmouseout", "ContextMenu_Out('" + this.id + "', event)");
    Common.setStyleAttribute(this.mainDiv, "position", "absolute");
    Common.setStyleAttribute(this.mainDiv, "width", this.width);
    Common.setStyleAttribute(this.mainDiv, "display", "none");
        
    var table = document.createElement("table");
    table.setAttribute("width", "100%");
    table.setAttribute("isContext", "yes");
        
    var tbody = document.createElement("tbody");
    tbody.setAttribute("isContext", "yes");
    table.appendChild(tbody);
    
    this.mainDiv.appendChild(table);
    
    var id;
    var desc;
    var item;
    var row;
    
    for(var i=0; i<this.count; i++) {
        id = this.items[i][0];
        desc = this.items[i][1];
        
        item = document.createElement("td");
        item.className = "item";
        item.setAttribute("isContext", "yes");
        item.setAttribute("isContext", "yes");
        item.innerHTML = desc;
        item.onmouseover = new Function("ContextMenu_Rollover('" + this.id + "', this)")
        item.onclick = new Function("ContextMenu_Accept('" + this.id + "', " + i + ")");
        
        row = document.createElement("<tr>");
        row.setAttribute("isContext", "yes");
        
        tbody.appendChild(row);
        
        row.appendChild(item);
    }
    
    document.body.appendChild(this.mainDiv);
    this.iswrited = true;

    this.setPosition(ev);
    this.mainDiv.style.display = "";
}

function ContextMenu_SetPosition(ev) 
{
	var mousePos  = Common.getMouseCoords(ev);
    Common.setPosition(this.mainDiv, mousePos.x - 10, mousePos.y - 10);
}


// ------------------------------------------------------------------------------------------------
// Funciones Externas
// ------------------------------------------------------------------------------------------------

function ContextMenu_Accept(id, index) 
{
    var contextMenu = ContextMenu_Instances[id];
    
    eval(contextMenu.acceptFunction + "(contextMenu.srcElement, contextMenu.items[index][0])");
    
    contextMenu.hide();
}

function ContextMenu_Rollover(id, item) 
{
    var contextMenu = ContextMenu_Instances[id];
    
    if(contextMenu.activeItem) {
        contextMenu.activeItem.className = "item";
    }
    
    item.className = "overItem";
    contextMenu.activeItem = item;
}

function ContextMenu_Out(id, ev)
{
	ev = ev || window.event;
    var contextMenu = ContextMenu_Instances[id];
    var srcElement = Common.getSrcElement(ev);
    var targetElement = Common.getTargetElement(ev);

    // chequeo que el evento out se disparo estando afuera del div
    if( srcElement.id == "mainDiv_" + id ) {
        contextMenu.hide();
    }
    else {
        // parche para cuando se escapa muy rapido
        if( (targetElement) && (!targetElement.getAttribute("isContext")) )
            contextMenu.hide();
    }
}


